package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.dto.ProductDTO;
import net.wanho.dto.ProductMassDTO;
import net.wanho.po.Product;
import net.wanho.service.ProductService;
import net.wanho.vo.AjaxResult;
import net.wanho.vo.PageInfo;
import net.wanho.vo.ProductVo;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequiredArgsConstructor
@RequestMapping("/product")
public class ProductController {

    private final ProductService productService;

    @GetMapping("/product-page-list")
    public AjaxResult productPageList(@RequestParam(defaultValue = "1") Integer pageNum,
                                      @RequestParam(defaultValue = "5") Integer pageSize,
                                      ProductDTO productDTO) {
        PageInfo<ProductVo> pageInfo = productService.productPageList(pageNum, pageSize, productDTO);

        return AjaxResult.success("success", pageInfo);
    }

    @PostMapping("/product-add")
    public AjaxResult productAdd(@RequestBody Product product) {

        productService.add(product);
        return AjaxResult.success("success");
    }
    //根据id查询商品
    @GetMapping("/product/{id}")
    public AjaxResult getProductById(@PathVariable Integer id) {
        Product product = productService.getById(id);
        return AjaxResult.success("success", product);
    }


    //编辑商品信息
    @PutMapping("/product-update")
    public AjaxResult productUpdate(@RequestBody ProductMassDTO productMassDTO) {
        productService.updateById(productMassDTO);
        return AjaxResult.success("商品修改成功");
    }






    //删除商品
    @DeleteMapping("/product-delete/{id}")
    public AjaxResult productDelete(@PathVariable Integer id) {
        productService.deleteById(id);
        return AjaxResult.success("商品删除成功");
    }


    //批量删除
    @DeleteMapping("/product-list-delete")
    public AjaxResult productDeleteList(@RequestBody Integer[] ids) {
        productService.removeBatchByIds(Arrays.asList(ids));
        return AjaxResult.success("商品批量删除成功");
    }
    //导出数据

    @GetMapping("/exportTable")
    public AjaxResult exportTable(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "5") Integer pageSize,
                                  ProductDTO productDTO) {
        PageInfo<ProductVo> pageInfo = productService.productPageList(pageNum, pageSize, productDTO);
        return AjaxResult.success("success", pageInfo);
    }
    //上架下架
    @PutMapping("state-change")
    public AjaxResult stateChange(@RequestBody ProductMassDTO productMassDTO) {
        productService.updateById(productMassDTO);
        return AjaxResult.success("商品状态修改成功");
    }

}
